/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
  \file			utils.c
  \date			September 2013
  \author		TNick

*//*


 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 Please read COPYING and README files in root folder
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */


#include <stdlib.h>     /* strtoull */
#include "utils.h"

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  DEFINITIONS    --------------------------------------------------------- */

/*  DEFINITIONS    ========================================================= */
//
//
//
//
/*  DATA    ---------------------------------------------------------------- */

/*  DATA    ================================================================ */
//
//
//
//
/*  FUNCTIONS    ----------------------------------------------------------- */

void
z_sleep (int msecs)
{
#if (defined (__WINDOWS__))
    Sleep (msecs);
#else
    struct timespec t;
    t.tv_sec  =  msecs / 1000;
    t.tv_nsec = (msecs % 1000) * 1000000;
    nanosleep (&t, NULL);
#endif
}

int64_t
z_clock (void)
{
#if (defined (__WINDOWS__))
    SYSTEMTIME st;
    GetSystemTime (&st);
    return (int64_t) st.wSecond * 1000 + st.wMilliseconds;
#else
    struct timeval tv;
    gettimeofday (&tv, NULL);
    return (int64_t) (tv.tv_sec * 1000 + tv.tv_usec / 1000);
#endif
}

void
log_message (const char *format, ...)
{
    time_t curtime = time (NULL);
    struct tm *loctime = localtime (&curtime);
    char formatted[64];
    strftime (formatted, 64, CCOD_FMAG "%y-%m-%d %H:%M:%S " CCOD_HC CCOD_FMAG, loctime);
    printf ("%s", formatted);

    va_list argptr;
    va_start (argptr, format);
    vprintf (format, argptr);
    va_end (argptr);
    printf (CCOD_RS "\n");
}

void
err_message (const char *format, ...)
{
    time_t curtime = time (NULL);
    struct tm *loctime = localtime (&curtime);
    char formatted[64];
    strftime (formatted, 64, CCOD_FMAG "%y-%m-%d %H:%M:%S " CCOD_FRED "ERROR! ", loctime);
    fprintf (stderr, "%s", formatted);

    va_list argptr;
    va_start (argptr, format);
    vfprintf (stderr, format, argptr);
    va_end (argptr);
    fprintf (stderr, CCOD_RS "\n");
}

#ifdef	LIBALC_DEBUG
void
dbg_message (const char *format, ...)
{
    time_t curtime = time (NULL);
    struct tm *loctime = localtime (&curtime);
    char formatted[64];
    strftime (formatted, 64, CCOD_FMAG "%y-%m-%d %H:%M:%S " CCOD_FMAG "DEBUG: ", loctime);
    printf ("%s", formatted);

    va_list argptr;
    va_start (argptr, format);
    vprintf (format, argptr);
    va_end (argptr);
    printf (CCOD_RS "\n");
}
#endif // LIBALC_DEBUG

func_error_t
alc_string_to_i64 (const char *str, ALC64S * out_int)
{
    char * p_end;
#if HAVE_STRTOLL==1
    signed long long int lli;
    lli = strtoll (str, &p_end, 10);
#elif HAVE_STRTOI64==1
    unsigned __int64 lli;
    lli = _strtoi64 (str, &p_end, 10);
#else
#   error   "No way to convert string to signed integer"
#endif
    if ( *p_end != 0 ) {
        return FUNC_GENERIC_ERROR;
    }
    *out_int = (ALC64S)lli;
    return FUNC_OK;
}

func_error_t
alc_string_to_u64 (const char *str, ALC64U * out_int)
{
    char * p_end;
#if HAVE_STRTOULL==1
    unsigned long long int ulli;
    ulli = strtoull (str, &p_end, 10);
#elif HAVE_STRTOUI64==1
    unsigned __int64 ulli;
    ulli = _strtoui64 (str, &p_end, 10);
#else
#   error   "No way to convert string to unsigned integer"
#endif
    if ( *p_end != 0 ) {
        return FUNC_GENERIC_ERROR;
    }
    *out_int = (ALC64U)ulli;
    return FUNC_OK;
}

void alc_sleep_seconds ( long seconds ) {
  clock_t limit, now = clock();
  limit = now + seconds * CLOCKS_PER_SEC;
  while ( limit > now )
    now = clock();
}

void alc_sleep_ticks ( long ticks ) {
  clock_t limit, now = clock();
  limit = now + ticks;
  while ( limit > now )
    now = clock();
}

void alc_wait(long milliseconds)
{
    long clk = clock();
    long timeout = clk + milliseconds;
    while( clock() < timeout ) {
        continue;
    }
}

/*  FUNCTIONS    =========================================================== */
//
//
//
//
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
